<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
        //判断SESSION里面有没有管理员ID
        if ($_SESSION['aid']!=null) {
            header("Location: ".U('admin/index/index')); 
        }

    	$this->display();
    }

    public function chacedata(){
        
    	//对比用户数据
    	$sql = "select * from user where email = ".'"'.I('post.username').'"'." and pass = ".'"'.md5(I('post.pass')).'"';
    	$user = M('user')->query($sql);
    	$user = current($user);
    	
    	if (!$user) {
    		$arr = array(
    			'error'=>0,
    			'info'=>'账号或密码错误'
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	//判断是否为管理员
    	if ($user['isadmin']==0) {
    		$arr = array(
    			'error'=>0,
    			'info'=>'非管理员'
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
            $_SESSION['aid']=$user['id'];
    		$arr = array(
    			'error'=>1,
    			'info'=>'登录成功',
    			'url'=>U('admin/index/index')
    			);
    		echo json_encode($arr);
    		exit;
    	}
    }

    public function loginout() {
        $_SESSION['aid']=null;
        header("Location: ".U('admin/login/index')); 
    }
}